﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace UnitHelper
{
    public class JsonHelper
    {
        /// <summary>
        /// 组装json字符串
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="list">数据集合</param>
        /// <param name="parameters">要组装的属性（可多个）</param>
        /// <returns>json串</returns>
        public static string ToJson<T>(List<T> list, params string[] parameters) where T : class
        {
            if (list.Count == 0)
            {
                return null;
            }
            if (parameters.Count() == 0)
            {
                return null;
            }
            else
            {
                string jsonString = "";
                foreach (T obj in list)
                {
                    var tempStr = "";
                    foreach (var parameter in parameters)
                    {
                        var data = obj.GetType().GetProperty(parameter).GetValue(obj, null);
                        if (tempStr == "")
                            tempStr += string.Format("\"{0}\":\"{1}\"", parameter, data);
                        else
                            tempStr += string.Format(",\"{0}\":\"{1}\"", parameter, data);
                    }
                    tempStr = "{" + tempStr + "}";
                    if (jsonString == "")
                    {
                        jsonString = tempStr;
                    }
                    else
                    {
                        jsonString += "," + tempStr;
                    }
                }
                if (jsonString != "")
                    return "{\"MyData\":[" + jsonString + "]}";
                else
                    return "{}";
            }
        }

        /// <summary>
        /// 组装json字符串
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="list">数据集合</param>
        /// <param name="parameters">要组装的属性（可多个）</param>
        /// <returns>json串</returns>
        public static string ForJson<T>(List<T> list, params string[] parameters) where T : class
        {
            if (list.Count == 0)
            {
                return null;
            }
            if (parameters.Count() == 0)
            {
                return null;
            }
            else
            {
                string jsonString = "";
                foreach (T obj in list)
                {
                    var tempStr = "";
                    foreach (var parameter in parameters)
                    {
                        var data = obj.GetType().GetProperty(parameter).GetValue(obj, null);
                        if (tempStr == "")
                            tempStr += string.Format("'{0}'", data);
                        else
                            tempStr += string.Format(",'{0}'", data);
                    }
                    tempStr = "[" + tempStr + "]";
                    if (jsonString == "")
                    {
                        jsonString = tempStr;
                    }
                    else
                    {
                        jsonString += "," + tempStr;
                    }
                }
                if (jsonString != "")
                    return "[" + jsonString + "]";
                else
                    return "[]";
            }
        }
    }
}
